
const actions = {}
const mutations = {
	getCariList(state, goods) {
		let result = state.cartList.findIndex(item => item.goods_id == goods.goods_id)
		if (result == -1) {
			if (!goods.goods_count) {
				goods.goods_count = 1
			}
			state.cartList.push(goods)
		} else {
			state.cartList[result].goods_count++
		}
		uni.setStorageSync('cartList',JSON.stringify(state.cartList))
	},
	getCheckState(state,goods) {
		state.cartList.map(item=> {
			if(item.goods_id == goods.goods_id) {
				item.goods_state = item.goods_state == 2 ? 0 : 2
				uni.setStorageSync('cartList',JSON.stringify(state.cartList))
			}
		})
	},
	getGoodsCount(state,obj) {
		state.cartList.map(item=> {
			if(item.goods_id == obj.id) {
				item.goods_count = parseInt(obj.num)
				uni.setStorageSync('cartList',JSON.stringify(state.cartList))
			}
		})
	},
	getCartState(state,type) {
		state.cartList.map(item=> {
			item.goods_state = type
		})
		uni.setStorageSync('cartList',JSON.stringify(state.cartList))
	}
}
const state = {
	cartList: JSON.parse(uni.getStorageSync('cartList') || '[]')
}
const getters = {
	total (state) {
		let num = 0
		state.cartList.forEach(item=> {
			num += item.goods_count
		})
		return num
	},
	checkAmount(state) {
		let arr = state.cartList.filter(item => {
			return item.goods_state
		})
		return arr.reduce((total,item) => {
			return total += item.goods_count
		},0)
	}
}

export default {
	namespaced: true,
	actions,
	mutations,
	state,
	getters
}